#include <pspsdk.h>
#include "led.h"

/*
 * Imports from library: sceSysEventForKernel
 */
extern void sceKernelRegisterSysEventHandler (int arg1);
extern void sceKernelUnregisterSysEventHandler (int arg1);

/*
 * Imports from library: InterruptManagerForKernel
 */
extern void InterruptManagerForKernel_668A0FF9 (int arg1, int arg2);
extern void InterruptManagerForKernel_CFFA1857 (int arg1, int arg2, int arg3, int arg4);
extern void sceKernelReleaseSubIntrHandler (int arg1, int arg2);
extern void sceKernelEnableSubIntr (int arg1, int arg2);

/*
 * Imports from library: SysclibForKernel
 */
extern void memset (int arg1, int arg2, int arg3);

/*
 * Imports from library: sceSyscon_driver
 */
extern void sceSysconCtrlLED (int arg1, int arg2);
extern int sceSyscon_driver_42E443D6 ();
extern int sceSyscon_driver_4B8F3502 ();
extern int sceSyscon_driver_8A65C819 ();

/*
 * Imports from library: sceGpio_driver
 */
extern void sceGpioPortClear (int arg1);
extern void sceGpioPortSet (int arg1);
extern void sceGpioSetPortMode (int arg1, int arg2);

/**
 * Subroutine at address 0x00000000
 */
void sceLedInit ()
{
  sp = sp + 0xFFFFFFE0;
  ((int *) sp)[0] = s0;
  ((int *) sp)[1] = s1;
  ((int *) sp)[4] = ra;
  ((int *) sp)[3] = s3;
  ((int *) sp)[2] = s2;
  memset (0x00000C10, 0x00000000, 0x000000F0);
  *((int *) 0x00000D00) = 0x00000002;
  *((char *) 0x00000C10) = 0x00000006;
  *((char *) 0x00000C12) = 0x00000000;
  *((char *) 0x00000C60) = 0x00000007;
  *((char *) 0x00000C62) = 0x00000001;
  var5 = sceSyscon_driver_8A65C819 ();
  var6 = var5 >> 0x00000010;
  var7 = var6 & 0x000000F0;
  if ((((0x00000000 < var7)) & ((0x00000000 < (var7 ^ 0x00000010)))) != 0x00000000)
  {
    *((char *) 0x00000C61) = 0x00000001;
  }
  if (var7 == 0x00000020)
  {
    var8 = *((int *) 0x00000D00);
    if (!((((var6 & 0x000000FF) < 0x0000002A)) != 0x00000000))
    {
      *((char *) 0x00000CB1) = 0x00000001;
      *((char *) 0x00000CB0) = 0x00000018;
      *((char *) 0x00000CB2) = 0x00000003;
      *((int *) 0x00000D00) = 0x00000003;

    label11:
      var8 = *((int *) 0x00000D00);
    }
  }
  else
  goto label11;
  var9 = 0x00000000;
  if (!(var8 <= 0))
  {
    var10 = 0x00000C10;
    while (1) {
      var11 = ((unsigned char *) var10)[0];
      var9 = var9 + 0x00000001;
      sceGpioSetPortMode (var11, 0x00000000);
      var14 = *((int *) 0x00000D00);
      var10 = var10 + 0x00000050;
      if (((var9 < var14)) != 0x00000000)
        continue;
      break;
    }
  }
  var15 = *((int *) 0x00000D00);
  var16 = 0x00000000;
  if (!(var15 <= 0))
  {
    var17 = 0x00000C10;
    while (1) {
      var18 = ((unsigned char *) var17)[1];
      if (var18 == 0x00000000)
      {
        var36 = ((unsigned char *) var17)[0];
        sceGpioPortClear ((0x00000001 << var36));
        var39 = ((unsigned char *) var17)[2];
      }
      else
      {
        var19 = ((unsigned char *) var17)[0];
        sceGpioPortSet ((0x00000001 << var19));
        var22 = ((unsigned char *) var17)[2];
      }
      var16 = var16 + 0x00000001;
      sceSysconCtrlLED (var22, 0x00000001);
      var25 = *((int *) 0x00000D00);
      var17 = var17 + 0x00000050;
      if (((var16 < var25)) != 0x00000000)
        continue;
      break;
    }
  }
  InterruptManagerForKernel_CFFA1857 (0x0000001E, 0x00000019, InterruptHandler, 0x00000000);
  sceKernelEnableSubIntr (0x0000001E, 0x00000019);
  sceKernelRegisterSysEventHandler (0x00000BD0);
  ra = ((int *) sp)[4];
  var32 = ((int *) sp)[3];
  var33 = ((int *) sp)[2];
  var34 = ((int *) sp)[1];
  var35 = ((int *) sp)[0];
  sp = sp + 0x00000020;
  return;
}

/**
 * Subroutine at address 0x00000198
 */
void sceLedSuspend ()
{
  sp = sp + 0xFFFFFFF0;
  ((int *) sp)[1] = ra;
  ((int *) sp)[0] = s0;
  InterruptManagerForKernel_668A0FF9 (0x0000001E, 0x00000019);
  sceGpioPortClear (0x00000040);
  sceGpioSetPortMode (0x00000006, 0x00000002);
  sceSysconCtrlLED (0x00000001, 0x00000000);
  var11 = sceSyscon_driver_42E443D6 ();
  if (var11 == 0x00000000)
  {
    sceGpioPortClear (0x00000080);
    sceGpioSetPortMode (0x00000006, 0x00000002);

  label24:
  }
  else
  {
    var12 = *((unsigned char *) 0x00000C61);
    if ((var12 ^ 0x00000001) == 0x00000000)
    {
      var34 = *((unsigned char *) 0x00000C60);
      sceGpioPortClear ((0x00000001 << var34));
    }
    else
    {
      var13 = *((unsigned char *) 0x00000C60);
      sceGpioPortSet ((0x00000001 << var13));
      goto label24;
    }
  }
  var16 = *((unsigned char *) 0x00000CB2);
  ra = ((int *) sp)[1];
  if (var16 != 0x00000000)
  {
    sceSysconCtrlLED (0x00000003, 0x00000000);
    var22 = sceSyscon_driver_4B8F3502 ();
    if (var22 == 0x00000000)
    {
      sceGpioPortClear (0x01000000);
      sceGpioSetPortMode (0x00000018, 0x00000002);
      ra = ((int *) sp)[1];
    }
    else
    {
      var23 = *((unsigned char *) 0x00000CB1);
      if ((var23 ^ 0x00000001) == 0x00000000)
      {
        var27 = *((unsigned char *) 0x00000CB0);
        sceGpioPortClear ((0x00000001 << var27));
        ra = ((int *) sp)[1];
      }
      else
      {
        var24 = *((unsigned char *) 0x00000CB0);
        sceGpioPortSet ((0x00000001 << var24));
        ra = ((int *) sp)[1];
      }
    }
  }
  var17 = ((int *) sp)[0];
  sp = sp + 0x00000010;
  return;
}
void InterruptHandler ()
{
  var1 = *((int *) 0x00000D00);
  var2 = 0x00000000;
  if (!(var1 <= 0)){
    var3 = 0x00000C10;
    var4 = *((int *) 0x00000C14);
    while (1) {
      var2 = var2 + 0x00000001;
      var3 = var3 + 0x00000050;
      if (var4 == 0x00000003)
      {
        sub_0040C (var3);
        var5 = *((int *) 0x00000D00);
      }
      else
      {
        var5 = *((int *) 0x00000D00);
      }
      if (!(((var2 < var5)) != 0x00000000))
        break;
      var4 = ((int *) var3)[1];
      continue;
    }
  }
  var6 = *((int *) 0x00000D00);
  var7 = 0x00000000;
  if (!(var6 <= 0))
  {
    var8 = 0x00000C10;
    while (1) {
      var9 = ((int *) var8)[5];
      if (var9 <= 0)
      {
        if (var9 != 0x00000000)
        {
          var10 = ((int *) var8)[2];
        }
        else
        {
          var27 = ((int *) var8)[6];
          ((int *) var8)[2] = 0x00000000;
          ((int *) var8)[3] = 0x00000000;
          ((int *) var8)[4] = 0x00000000;
          if (var27 != 0x00000000)
          {
            ((int *) var8)[3] = 0x00000001;
          }
          else
          {
            ((int *) var8)[4] = 0x00000001;
          }

        label20:
          var10 = ((int *) var8)[2];
        }
      }
      else
      {
        ((int *) var8)[5] = (var9 + 0xFFFFFFFF);
        goto label20;
      }
      var11 = ((int *) var8)[3];
      var12 = ((int *) var8)[4];
      var13 = var10 + 0x00000001;
      ((int *) var8)[2] = var13;
      if (!(((var13 < (var11 + var12))) != 0x00000000))
      {
        ((int *) var8)[2] = 0x00000000;
      }
      var14 = ((int *) var8)[2];
      var15 = ((unsigned char *) var8)[1];
      if (((var14 < var11)) == var15)
      {
        var24 = ((unsigned char *) var8)[0];
        sceGpioPortClear ((0x00000001 << var24));
        var19 = *((int *) 0x00000D00);
      }
      else
      {
        var16 = ((unsigned char *) var8)[0];
        sceGpioPortSet ((0x00000001 << var16));
        var19 = *((int *) 0x00000D00);
      }
      var7 = var7 + 0x00000001;
      var8 = var8 + 0x00000050;
      if (((var7 < var19)) != 0x00000000)
        continue;
      break;
    }
  }
  ra = ((int *) sp)[4];
  var20 = ((int *) sp)[3];
  var21 = ((int *) sp)[2];
  var22 = ((int *) sp)[1];
  var23 = ((int *) sp)[0];
  sp = sp + 0x00000020;
  return;
}

/**
 * Subroutine at address 0x0000040C
 */
void sub_0040C (int arg1)
{
  var1 = ((int *) arg1)[8];
  var2 = arg1;
  if (var1 <= 0)
  {

  label4:
    var4 = ((int *) var2)[9];
    if (var4 == 0x00000000)
    {
      ((int *) var2)[9] = var4;
    }
    else
    {
      var5 = ((unsigned char *) var4)[0];
      while (1) {
        var4 = var4 + 0x00000001;
        if (((char) var5) < 0)
        {
          ((int *) var2)[8] = (var5 & 0x0000007F);
        }
        else
        {
          if (((var5 < 0x00000014)) == 0x00000000)
          {
            var4 = 0x00000000;

          label32:
            if (!(var4 != 0x00000000))
              break;
            var5 = ((unsigned char *) var4)[0];
            continue;
          }
          else
          {
            var6 = ((int *) ((var5 << 0x00000002) + 0x00000B74))[0];
            switch () {
            case 0:
              var4 = 0x00000000;
              goto label35;
            case 1:
              ((int *) var2)[10] = var4;
              goto label32;
            case 2:
              var4 = ((int *) var2)[10];
              goto label32;
            case 3:
              var7 = ((int *) var2)[11];
              if (((var7 < 0x00000004)) == 0x00000000)
              {
                var4 = 0x00000000;
              }
              else
              {
                var8 = ((unsigned char *) var4)[0];
                ((int *) ((var7 << 0x00000003) + var2))[13] = var8;
                var4 = var4 + 0x00000001;
                var9 = ((int *) var2)[11];
                ((int *) ((var9 << 0x00000003) + var2))[12] = var4;
                ((int *) var2)[11] = (var9 + 0x00000001);
                goto label32;
              }
            case 4:
              var10 = ((int *) var2)[11];
              if (var10 <= 0)
              {
                var4 = 0x00000000;
              }
              else
              {
                var11 = (var10 << 0x00000003) + var2;
                var12 = ((int *) var11)[11];
                ((int *) var11)[11] = (var12 + 0xFFFFFFFF);
                var13 = ((int *) var2)[11];
                var14 = (var13 << 0x00000003) + var2;
                var15 = ((int *) var14)[11];
                if (var15 <= 0)
                {
                  ((int *) var2)[11] = (var13 + 0xFFFFFFFF);
                }
                else
                {
                  var4 = ((int *) var14)[12];
                }
                goto label32;
              }
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
            case 11:
            case 12:
            case 13:
            case 14:
            case 15:
              var4 = 0x00000000;
              goto label32;
            case 16:
              ((int *) var2)[3] = 0x00000001;
              ((int *) var2)[7] = 0x00000001;

            label20:
              ((int *) var2)[4] = 0x00000000;
              goto label32;
            case 17:
              ((int *) var2)[4] = 0x00000001;
              ((int *) var2)[7] = 0x00000000;

            label18:
              ((int *) var2)[3] = 0x00000000;
              goto label32;
            case 18:
              var16 = ((int *) var2)[7];
              if (var16 == 0x00000001)
              {
                ((int *) var2)[3] = 0x00000001;
                ((int *) var2)[7] = 0x00000000;
                goto label20;
              }
              else
              {
                ((int *) var2)[4] = 0x00000001;
                ((int *) var2)[7] = 0x00000001;
                goto label18;
              }
              goto label32;
            case 19:
              ((int *) var2)[7] = 0x00000002;
              var17 = var4 + 0x00000001;
              var18 = ((unsigned char *) var4)[0];
              var4 = var17 + 0x00000001;
              ((int *) var2)[3] = var18;
              var19 = ((unsigned char *) var17)[0];
              ((int *) var2)[4] = var19;
              goto label32;
            }
          }
        }
      }

    label35:
      ((int *) var2)[9] = var4;
    }
  }
  else
  {
    var3 = var1 + 0xFFFFFFFF;
    ((int *) arg1)[8] = var3;
    if (var3 <= 0)
      goto label4;
  }
  return;
}

/**
 * Subroutine at address 0x000005A0
 */
void sceLedSetMode (int arg1, int arg2, int arg3)
{
  if (arg1 < 0)
  {

  label23:
    var2 = 0x80000102;
  }
  else
  {
    var1 = *((int *) 0x00000D00);
    if (((arg1 < var1)) != 0x00000000)
    {
      var2 = 0x80000107;
      if (!(((arg2 < 0x00000004)) == 0x00000000))
      {
        var3 = (((arg1 << 0x00000002) + arg1) << 0x00000004) + 0x00000C10;
        ((int *) var3)[5] = 0xFFFFFFFF;
        ((int *) var3)[9] = 0x00000000;
        var4 = ((int *) var3)[1];
        ((int *) var3)[1] = arg2;
        if (arg2 == 0x00000001)
        {
          ((int *) var3)[3] = arg2;
          ((int *) var3)[4] = 0x00000000;
          ((int *) var3)[2] = 0x00000000;

        label21:
          var2 = 0x00000000;
        }
        else
        {
          if (((arg2 < 0x00000002)) != 0x00000000)
          {
            if (arg2 != 0x00000000)
            {
              var2 = 0x00000000;
            }
            else
            {
              ((int *) var3)[4] = 0x00000001;
              ((int *) var3)[2] = 0x00000000;
              ((int *) var3)[3] = 0x00000000;
              goto label21;
            }
          }
          else
          {
            if (arg2 == 0x00000002)
            {
              if (var4 != arg2)
              {
                ((int *) var3)[2] = 0x00000000;
              }
              var5 = ((int *) arg3)[1];
              ((int *) var3)[3] = var5;
              var6 = ((int *) arg3)[2];
              ((int *) var3)[4] = var6;
              var7 = ((int *) arg3)[3];
              ((int *) var3)[5] = var7;
              var8 = ((int *) arg3)[4];
              ((int *) var3)[6] = var8;
            }
            else
            {
              if (arg2 == 0x00000003)
              {
                if (var4 != arg2)
                {
                  ((int *) var3)[7] = var4;
                }
                ((int *) var3)[8] = 0x00000001;
                ((int *) var3)[9] = arg3;
                ((int *) var3)[11] = 0x00000000;
                ((int *) var3)[2] = 0x00000000;
                ((int *) var3)[10] = 0x00000000;
              }
            }
            goto label21;
          }
        }
      }
    }
    else
    {
      goto label23;
    }
  }
  return;
}
void module_start (){
  sceLedInit ();
}
void module_reboot_before (){
  var1 = 0x00000000;
  sceKernelUnregisterSysEventHandler (0x00000BD0);
  sceKernelReleaseSubIntrHandler (0x0000001E, 0x00000019);
	var7 = 0x00000C10;
	while (var1 < *((int *) 0x00000D00)) {
		if (((unsigned char *) var7)[1]){
			sceGpioPortSet ((0x00000001 << var9 = ((unsigned char *) var7)[0]));
		}else{
			sceGpioPortClear ((0x00000001 << ((unsigned char *) var7)[0]));
		}
		var1++;
		var7+=0x50;
	}
}

/**
 * Subroutine at address 0x0000076C
 */
void sceLedEnd ()
{
  sp = sp + 0xFFFFFFE0;
  ((int *) sp)[4] = ra;
  ((int *) sp)[3] = s3;
  ((int *) sp)[1] = s1;
  var1 = 0x00000000;
  ((int *) sp)[2] = s2;
  ((int *) sp)[0] = s0;
  sceKernelUnregisterSysEventHandler (0x00000BD0);
  sceKernelReleaseSubIntrHandler (0x0000001E, 0x00000019);
  var6 = *((int *) 0x00000D00);
  if (!(var6 <= 0))
  {
    var7 = 0x00000C10;
    while (1) {
      var8 = ((unsigned char *) var7)[1];
      if (var8 == 0x00000000)
      {
        var17 = ((unsigned char *) var7)[0];
        sceGpioPortClear ((0x00000001 << var17));
        var12 = *((int *) 0x00000D00);
      }
      else
      {
        var9 = ((unsigned char *) var7)[0];
        sceGpioPortSet ((0x00000001 << var9));
        var12 = *((int *) 0x00000D00);
      }
      var1 = var1 + 0x00000001;
      var7 = var7 + 0x00000050;
      if (((var1 < var12)) != 0x00000000)
        continue;
      break;
    }
  }
  ra = ((int *) sp)[4];
  var13 = ((int *) sp)[3];
  var14 = ((int *) sp)[2];
  var15 = ((int *) sp)[1];
  var16 = ((int *) sp)[0];
  sp = sp + 0x00000020;
  return;
}

/**
 * Subroutine at address 0x00000814
 */
void sceLedResume ()
{
  sp = sp + 0xFFFFFFE0;
  ((int *) sp)[3] = s3;
  var1 = *((int *) 0x00000D00);
  ((int *) sp)[1] = s1;
  var2 = 0x00000000;
  ((int *) sp)[4] = ra;
  ((int *) sp)[2] = s2;
  ((int *) sp)[0] = s0;
  if (!(var1 <= 0))
  {
    var3 = 0x00000C10;
    var4 = *((unsigned char *) 0x00000C11);
    while (1) {
      if (var4 == 0x00000000)
      {
        var21 = ((unsigned char *) var3)[0];
        sceGpioPortClear ((0x00000001 << var21));
        var24 = ((unsigned char *) var3)[0];
      }
      else
      {
        var5 = ((unsigned char *) var3)[0];
        sceGpioPortSet ((0x00000001 << var5));
        var8 = ((unsigned char *) var3)[0];
      }
      var2 = var2 + 0x00000001;
      sceGpioSetPortMode (var8, 0x00000000);
      var11 = ((unsigned char *) var3)[2];
      var3 = var3 + 0x00000050;
      sceSysconCtrlLED (var11, 0x00000001);
      var14 = *((int *) 0x00000D00);
      if (!(((var2 < var14)) != 0x00000000))
        break;
      var4 = ((unsigned char *) var3)[1];
      continue;
    }
  }
  sceKernelEnableSubIntr (0x0000001E, 0x00000019);
  ra = ((int *) sp)[4];
  var17 = ((int *) sp)[3];
  var18 = ((int *) sp)[2];
  var19 = ((int *) sp)[1];
  var20 = ((int *) sp)[0];
  sp = sp + 0x00000020;
  return;
}

/**
 * Subroutine at address 0x000008CC
 */
void sub_008CC (int arg1)
{
  sp = sp + 0xFFFFFFF0;
  ((int *) sp)[0] = ra;
  if (arg1 == 0x0000400A)
  {
    sceLedSuspend ();
    ra = ((int *) sp)[0];
  }
  else
  {
    if (arg1 == 0x00100000)
    {
      sceLedResume ();
      ra = ((int *) sp)[0];
    }
    else
    {
      ra = ((int *) sp)[0];
    }
  }
  sp = sp + 0x00000010;
  return;
}

